import Vue from 'vue'
import Cookies from 'js-cookie'
import 'normalize.css/normalize.css' // a modern alternative to CSS resets
import Element from 'element-ui'
import './styles/element-variables.scss'
import '@/styles/index.scss' // global css
import App from './App'
import store from './store'
import router from './router'
import './icons' // icon
import './permission' // permission control
import './utils/error-log' // error log
import * as filters from './filters' // global filters
import { getDictArray, getDictLabel } from './api/attendance/dict-service'
import { getLoginUser } from './api/attendance/authorization-service'

Vue.prototype.loginUser = async function() {
  console.log('userlogin log:')
  await getLoginUser().then(res => {
    try {
      const data = res.data
      localStorage.setItem('empId', data.empId)
      localStorage.setItem('empCode', data.empCode)
      localStorage.setItem('empName', data.empName)
      localStorage.setItem('deptName', data.deptName)
      localStorage.setItem('deptCode', data.deptCode)
      localStorage.setItem('orgCode', data.orgCode)
      localStorage.setItem('orgName', data.orgName)
      localStorage.setItem('orgId', data.orgId)
      console.log('localStorage log:' + data)
    } catch (e) {
      console.log(e)
    }
  })
  // return userData.data
  console.log('userlogin get emp:' + localStorage.getItem('empCode'))
}
Vue.prototype.loginUser()

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
import { mockXHR } from '../mock'
if (process.env.NODE_ENV === 'production') {
  mockXHR()
}

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false
Vue.prototype.getDictArray = function(dicttypeId) {
  return getDictArray(dicttypeId)
}
Vue.prototype.getDictLabel = function(dicttypeId, dictId) {
  return getDictLabel(dicttypeId, dictId)
}
Vue.prototype.dictFormatter = function(row, column) {
  const dictTypeId = column.labelClassName
  const propName = column.property
  const dictId = row[propName]

  const renderLabel = getDictLabel(dictTypeId, dictId)
  // console.log('propName:' + propName + ' dictTypeId:' + dictTypeId + ' dictId : ' + dictId + ' renderLabel:' + renderLabel)

  return renderLabel
}

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
